import Vue from 'vue'
import Vuex from 'vuex'
import { getDate } from '@/utils'
import uuidV1 from '@/utils/uuidv1.min'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
    state: {//存放状态
        uuid: uuidV1(),
        username: '',
        student_number: '',
        major: '',
        curriculum: '',
        teacher: '',
        score: '',
		time: '',
    },
    getters: {
        data(state) {
			const time = state.time || getDate();
            return JSON.stringify({ ...state, time });
        },
    },
    mutations: {
        saveUser(state, { username, student_number }) {
            state.username = username;
            state.student_number = student_number;
        },
		saveTime(state, time) {
			state.time = time;
		},
        saveMajor(state, major) {
            state.major = major;
        },
        saveCurriculum(state, curriculum) {
            state.curriculum = curriculum;
        },
        saveTeacher(state, teacher) {
            state.teacher = teacher;
        },
        saveScore(state, score) {
            state.score = score;
        },
        resetState(state, data) {
            try {
                data && Object.assign(state, JSON.parse(data));
            } catch (error) {
                console.error(error);
                // catch error   
            }
        }
    },
    actions: {
        submit({ getters }) {
            uni.request({
                url: getApp().globalData.baseUrl + 'static/php/score.php',
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded',
                },
                data: JSON.parse(getters.data),
                success: (res) => {
                    this.list = res.data.list;
                },
            });
        },
    },
})
export default store